package com.yuuzy.dao;

import com.alibaba.druid.util.StringUtils;
import com.yuuzy.model.Insurance;

import java.util.List;

public class InsuranceDAO extends BasicDAO<Insurance> {
    /**
     * 查询保险信息
     *
     * @param insurance 保险实体
     * @return 查询到的保险实体集
     */
    public List<Insurance> queryInsurance(Insurance insurance) {
        StringBuilder sb = new StringBuilder("select * from insurance");//查询名称为？的保险在insurance表中的信息
        if (!StringUtils.isEmpty(insurance.getInsurance_name())) {
            sb.append(" and insurance_name like '%").append(insurance.getInsurance_name()).append("%'");//支持模糊查询
        }
        if (insurance.getInsurance_id() != null) {
            sb.append(" and insurance_id =").append(insurance.getInsurance_id());
        }
        String sql = sb.toString().replaceFirst("and", "where");

        return queryMulti(sql, Insurance.class);
    }

    /**
     * 增加保险
     *
     * @param insurance 增加的保险实体
     * @return 受影响的行数
     */
    public int insertInsurance(Insurance insurance) {
        //insurance_id自增
        String sql = "INSERT INTO finance.insurance(insurance_name,insurance_project,insurance_person,insurance_amount,insurance_year,insurance_up) VALUES (?,?,?,?,?,?)";//添加保险信息
        return update(sql, insurance.getInsurance_name(), insurance.getInsurance_project(), insurance.getInsurance_person(), insurance.getInsurance_amount(), insurance.getInsurance_year(), insurance.getInsurance_up());
    }

    /**
     * 删除保险
     *
     * @param insurance 待删除的保险实体
     * @return 受影响的行数
     */
    public int deleteInsurance(Insurance insurance) {
        StringBuilder sb = new StringBuilder("DELETE FROM insurance");//删除id和名称相匹配的保险(两者至少有一个）
        if (!StringUtils.isEmpty(insurance.getInsurance_name())) {
            sb.append(" and insurance_name = '").append(insurance.getInsurance_name()).append("'");
        }
        if (insurance.getInsurance_id() != 0) {
            sb.append(" and insurance_id = '").append(insurance.getInsurance_id()).append("'");
        }
        String sql = sb.toString().replaceFirst("and", "where");
        return update(sql);
    }

    /**
     * 更改保险信息
     *
     * @param insurance 待更改的保险实体
     * @return 受影响的行数
     */
    public int updateInsurance(Insurance insurance) {
        //id不能更改
        StringBuilder sb = new StringBuilder("UPDATE insurance SET");
        if (!StringUtils.isEmpty(insurance.getInsurance_name())) {
            //如果名称字段存在
            sb.append(" ,insurance_name = '").append(insurance.getInsurance_name()).append("'");
        }
        if (!StringUtils.isEmpty(insurance.getInsurance_project())) {
            //如果项目字段存在
            sb.append(" ,insurance_project = '").append(insurance.getInsurance_project()).append("'");
        }
        if (!StringUtils.isEmpty(insurance.getInsurance_person())) {
            //如果适用人群字段存在
            sb.append(" ,insurance_person = '").append(insurance.getInsurance_person()).append("'");
        }
        if (insurance.getInsurance_amount() != null) {
            //如果保险金额字段存在
            sb.append(" ,insurance_amount = ").append(insurance.getInsurance_amount());
        }
        if (insurance.getInsurance_year() != null) {
            //如果保险年限字段存在
            sb.append(" ,insurance_year = ").append(insurance.getInsurance_year());
        }
        if (!StringUtils.isEmpty(insurance.getInsurance_up())) {
            //如果标志位字段存在
            sb.append(" ,insurance_up = '").append(insurance.getInsurance_up()).append("'");
        }
        String sql = sb.append(" WHERE insurance_id = ").append(insurance.getInsurance_id()).toString().replaceFirst(",", "");
        return update(sql);
    }

    /**
     * 保险上线功能
     *
     * @param insurance 待上线的保险实体
     * @return 设置为上线状态的保险实体
     */
    private Insurance upInsurance(Insurance insurance) {
        insurance.setInsurance_up("1");
        return insurance;
    }

    /**
     * 保险下线功能
     *
     * @param insurance 待下线的保险实体
     * @return 设置为下线状态的保险实体
     */
    private Insurance offInsurance(Insurance insurance) {
        insurance.setInsurance_up("0");
        return insurance;
    }

    /**
     * 实现保险的上下线功能
     *
     * @param insurance 待上线/下线的保险实体
     * @return 已下线/上线的保险实体
     */
    public Insurance up_OR_offInsurance(Insurance insurance) {
        if (insurance.getInsurance_up().equals("1")) {
            //将上线状态转换为下线状态
            return offInsurance(insurance);
        }
        if (insurance.getInsurance_up().equals("0")) {
            //将下线状态转换为上线状态
            return upInsurance(insurance);
        }
        System.out.println("错误，标志位不正确！");
        return null;
    }
}
